package com.bjfu.eduservice.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjfu.common_utils.JwtUtils;
import com.bjfu.common_utils.R;
import com.bjfu.common_utils.vo.CourseWebVo;
import com.bjfu.eduservice.client.OrdersClient;
import com.bjfu.eduservice.entity.EduCourse;

import com.bjfu.eduservice.entity.chapter.ChapterVo;
import com.bjfu.eduservice.entity.frontvo.CourseFrontVo;
import com.bjfu.eduservice.service.EduChapterService;
import com.bjfu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
//@CrossOrigin
@Api("课程前台管理")
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrdersClient ordersClient;

    @ApiOperation(value = "根据条件获取课程分页列表")
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page,@PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo ){
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "获取前台课程详细信息")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        // 获取课程详情
        CourseWebVo courseWebVo = courseService.getCourseFrontInfo(courseId);

        //获取章节列表
        List<ChapterVo> ChapterVoList = chapterService.getChapterListByCourseId(courseId);

        if(!StringUtils.isEmpty(request.getHeader("token"))){
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            boolean isBuyCourse = ordersClient.isBuyCourse(courseId, memberId);
            return R.ok().data("courseInfo",courseWebVo).data("chapterList",ChapterVoList).data("isbuyCourse",isBuyCourse);
        }else {
            return R.ok().data("courseInfo",courseWebVo).data("chapterList",ChapterVoList);
        }
    }

    @GetMapping("getCourseInfoClient/{id}")
    public CourseWebVo getCourseInfoClient(@PathVariable String id){
        CourseWebVo courseFrontInfo = courseService.getCourseFrontInfo(id);
        return courseFrontInfo;
    }
}
